<template>
  <div>
    <el-button type="primary" @click="exportTable">导出</el-button>
    <el-table :data="tableData">
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
    </el-table>
  </div>
</template>

<script>
import * as XLSX from 'xlsx'; // 引入下载表格
import FileSaver from 'file-saver';
export default {
  data() {
    return {
      tableData: [
        { name: '张三', age: 18, address: '北京市' },
        { name: '李四', age: 20, address: '上海市' },
        { name: '王二', age: 17, address: '上海市' },
        { name: '麻子', age: 22, address: '深圳市' }
      ]
    };
  },
  methods: {
    exportTable() {
      console.log(XLSX);
      let newdata= this.tableData
      newdata.forEach(i => {
        delete i.address
        for (let key in i){
          console.log(key);
       }
      });
      const worksheet = XLSX.utils.json_to_sheet(this.tableData);
      const workbook = XLSX.utils.book_new();
      XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');
      const excelBuffer = XLSX.write(workbook, {
        bookType: 'xlsx',
        type: 'array'
      });
      const blob = new Blob([excelBuffer], { type: 'application/vnd.ms-excel' });
      FileSaver.saveAs(blob, 'table'); // 下载文件 文件名
    }
  }
};
</script>
